﻿using MESMian.EntityMapping;
using Microsoft.EntityFrameworkCore;
using Model;
using System.Configuration;

namespace MESMian.Context
{
    public class MyContext : DbContext
    {
        public MyContext() { }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            string connstring = ConfigurationManager.ConnectionStrings["ConnDatabase"].ConnectionString;
            optionsBuilder.UseSqlServer(connstring);//EntityFramework SqlServer
            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {   //推荐这种写法
            modelBuilder.ApplyConfiguration(new UsersMapping());
            modelBuilder.ApplyConfiguration(new ProductSpecsMapping());
            modelBuilder.ApplyConfiguration(new ProductsMapping());
            modelBuilder.ApplyConfiguration(new ProductionPlansMapping());
            modelBuilder.ApplyConfiguration(new ProductionPlanDetailsMapping());
            modelBuilder.ApplyConfiguration(new MaterialReceivesMapping());
            modelBuilder.ApplyConfiguration(new MaterialReceiveDetailsMapping());
            modelBuilder.ApplyConfiguration(new StoresMapping());
            modelBuilder.ApplyConfiguration(new TeamsMapping());
            modelBuilder.ApplyConfiguration(new TeamMembersMapping());
            modelBuilder.ApplyConfiguration(new ProductionLineCategoriesMapping());
            modelBuilder.ApplyConfiguration(new ProductionLinesMapping());
            modelBuilder.ApplyConfiguration(new WorkShopCategoriesMapping());
            modelBuilder.ApplyConfiguration(new WorkShopsMapping());
            modelBuilder.ApplyConfiguration(new DeviceCategoriesMapping());
            modelBuilder.ApplyConfiguration(new DeviceSpecsMapping());
            modelBuilder.ApplyConfiguration(new DevicesMapping());
            modelBuilder.ApplyConfiguration(new DeviceMaintenancesMapping());
            modelBuilder.ApplyConfiguration(new DeviceInspectionsMapping());
            modelBuilder.ApplyConfiguration(new ProductionProcessesMapping());
            modelBuilder.ApplyConfiguration(new ProductionTechnologiesMapping());
            modelBuilder.ApplyConfiguration(new ProductionTechnologyDetailsMapping());
            modelBuilder.ApplyConfiguration(new StoreCategoriesMapping());
            modelBuilder.ApplyConfiguration(new ProductCategoriesMapping());

            #region Units 表的创建 (不推荐)
            modelBuilder.Entity<Units>()
                .ToTable("Uints")
                .HasKey(u => u.UintId);

            modelBuilder.Entity<Units>()
                .Property(u => u.UnitName)
                .HasColumnType("varchar(30)")
                .IsRequired();
            modelBuilder.Entity<Units>().Property(u => u.Status)
                .IsRequired()
                .HasColumnType("int");
            modelBuilder.Entity<Units>().Property(u => u.CreatUserId)
                .IsRequired(true)
                .HasColumnType("int");
            modelBuilder.Entity<Units>().Property(u => u.CreateTime)
                .IsRequired()
                .HasDefaultValueSql("GETDATE()")
                .HasColumnType("datetime");
            modelBuilder.Entity<Units>().Property(u => u.LastUpdateTime)
                .HasColumnType("dateTime");
            modelBuilder.Entity<Units>().Property(u => u.LastUpdateUserId)
                .HasColumnType("int");
            #endregion
        }

        public virtual DbSet<Units> Units { get; set; }
        public virtual DbSet<StoreCategories> StoreCategories { get; set; }

        public virtual DbSet<Users> Users { get; set; }
        public virtual DbSet<ProductSpecs> ProductSpecs { get; set; }
        public virtual DbSet<ProductCategories> ProductCategories { get; set; }
        public virtual DbSet<Products> Products { get; set; }
        public virtual DbSet<ProductionPlans> ProductionPlans { get; set; }
        public virtual DbSet<ProductionPlanDetails> ProductionPlanDetails { get; set; }
        public virtual DbSet<MaterialReceives> MaterialReceives { get; set; }
        public virtual DbSet<MaterialReceiveDetails> MaterialReceiveDetails { get; set; }
        public virtual DbSet<Stores> Stores { get; set; }
        public virtual DbSet<Teams> Teams { get; set; }
        public virtual DbSet<TeamMembers> TeamMembers { get; set; }
        public virtual DbSet<ProductionLineCategories> ProductionLineCategories { get; set; }
        public virtual DbSet<ProductionLines> ProductionLines { get; set; }
        public virtual DbSet<WorkShopCategories> WorkShopCategories { get; set; }
        public virtual DbSet<WorkShops> WorkShops { get; set; }
        public virtual DbSet<DeviceCategories> DeviceCategories { get; set; }
        public virtual DbSet<DeviceSpecs> DeviceSpecs { get; set; }
        public virtual DbSet<Devices> Devices { get; set; }
        public virtual DbSet<DeviceMaintenances> DeviceMaintenances { get; set; }
        public virtual DbSet<DeviceInspections> DeviceInspections { get; set; }
        public virtual DbSet<ProductionProcesses> ProductionProcesses { get; set; }
        public virtual DbSet<ProductionTechnologies> ProductionTechnologies { get; set; }
        public virtual DbSet<ProductionTechnologyDetails> ProductionTechnologyDetails { get; set; }

    }
}
